package com.examination.mlib.manager;

import android.annotation.SuppressLint;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

import androidx.core.app.NotificationCompat;

import com.alibaba.fastjson.JSON;
import com.examination.mlib.R;

import org.jetbrains.annotations.NotNull;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by SHICHENG
 * <p>
 * Time on 2022/02/16
 */
public class LocalNotificatioManager {

    private static final int NO_1 =0x1 ;

    private static LocalNotificatioManager LocalNotificationManager = new LocalNotificatioManager();

    private static NotificationCompat.Builder builder;

    public static LocalNotificatioManager getInstance(Context context){
        if (builder == null){
            builder = new NotificationCompat.Builder(context);
        }
        return LocalNotificationManager;
    }

    public LocalNotificatioManager NotificationStyle(@NotNull String title, @NotNull String content){

        builder.setSmallIcon(R.mipmap.app_logo);
        builder.setContentTitle(title);
        builder.setChannelId("1");
        builder.setContentText(content);
        builder.setPriority(NotificationManager.IMPORTANCE_DEFAULT);
        //设置Notification.Default_ALL(默认启用全部服务(呼吸灯，铃声等)
        builder.setDefaults(Notification.DEFAULT_ALL);
        return this;
    }

    public LocalNotificatioManager NotificationIntent(Context context, Class<?> c, String params){
        Intent intent = new Intent(context, c);
        Map<String, Object> paramsMap = (Map<String, Object>) JSON.parse(params);
        Set<Map.Entry<String, Object>> entries = paramsMap.entrySet();
        Iterator<Map.Entry<String, Object>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> next = iterator.next();
            intent.putExtra(next.getKey(), next.getValue().toString());
        }
        PendingIntent pi = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pi);
        return this;
    }

    public void NotificationIM(Context context,int notifyId){
        //获取Notification
         Notification n = builder.build();
        //通过NotificationCompat.Builder.build()来获得notification对象自己
        @SuppressLint("ServiceCast")
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            String id = "IM_MESSAGE";
            String name = "问诊消息通知";
            NotificationChannel channel = new NotificationChannel(id,name, NotificationManager.IMPORTANCE_HIGH);
            manager.createNotificationChannel(channel);
        }
        //然后调用NotificationManager.notify()向系统转交
        manager.notify(notifyId,n);
    }

    public void cancelNotification(Context context,int id){
        @SuppressLint("ServiceCast")
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        manager.cancel(id);
    }
}
